﻿module ClaimInput
open IntelliFactory.WebSharper
open IntelliFactory.WebSharper.Html
open IntelliFactory.WebSharper.JQuery
open IntelliFactory.WebSharper.JQuery.Mobile
open IntelliFactory.Formlet.Base
open IntelliFactory.WebSharper.Formlet
open IntelliFactory.WebSharper.JQuery.Mobile.Enums
open System
open Claims


[<JavaScript>]
let GetPosition() : (Async<Html5.Position>) =
    Async.FromContinuations(fun (onOk, _, _) ->
        Html5.Window.Self.Navigator.Geolocation.GetCurrentPosition(fun pos ->
            onOk pos
        )
    )

[<JavaScript>]
let LoadPolicies() =
    async {
        let! policies = Policies.getPoliciesAsync()
        let children =
            Div[] -< (
                    [Legend [Text "Policy"]]
                    @
                    [for p in policies do   
                        yield Input [Attr.Type "radio"; Attr.Name "pchoice"; Id ("p"+p.Id); Attr.Value p.Id]
                        yield Label [Attr.For ("p" + p.Id)] -< [Text p.Type]
                        ]
                    @ [P[]]
                       )
        JQuery.Of("#policychoice").Html(children.Html) |> ignore
        JQuery.Of("#newClaim").Children(":jqmData(role=content)").Trigger("create") |> ignore
    }

type ClaimFormData = 
    {
        mutable Location:Location
        mutable Description:string option
        mutable PolicyId:string option
    }

[<JavaScript>]
let CurrentClaimForm = {Location={Lat=0.;Lon=0.}; Description=None; PolicyId=None}

[<JavaScript>]
let ClaimIputPage () = 
    //values set by the form

    let header =
        Div [HTML5.Attr.Data "role" "header"; HTML5.Attr.Data "theme" "b"] -< [ 
                       
                A [ HRef "#"; HTML5.Attr.Data "role" "button"; 
                    HTML5.Attr.Data "icon" "delete"
                    HTML5.Attr.Data "inline" "true"
                ] -< [Text "Cancel"]
                |>! OnClick (fun _ _ -> JQuery.Of("#claims") |> Nav.transitionWith Nav.SlideLeft)

                H1 [Text "New Claim"]

                A [ HRef "#"; HTML5.Attr.Data "role" "button"; 
                    HTML5.Attr.Data "icon" "check"
                    HTML5.Attr.Data "inline" "true"
                ] -< [Text "Save"]
                |>! OnClick (fun _ _ -> 
                    match CurrentClaimForm.PolicyId,CurrentClaimForm.Description with
                    | Some pid, Some desc ->
                        let claim =
                            {Id=""; PolicyId = pid
                             Location=CurrentClaimForm.Location
                             PolicyType=""; Description=desc
                             Status="open" }
                        saveClaim claim
                        Nav.showNotification("New claim saved","#claims")
                           
                    | _,_ ->
                          Nav.showDialog("Policy and description are required") 
                    )
            ]

    let content =
        Div [HTML5.Attr.Data "role" "content"] -< [

                //policy selection - data filled asynchronously
                FieldSet [HTML5.Attr.Data "role" "controlgroup"; Id "policychoice"] 
                |>! OnChange (fun d  -> 
                    let v = JQuery.Of("input[type=radio]:checked").Val()
                    CurrentClaimForm.PolicyId <- Some (string v)
                    )

                //gps location
                Div [HTML5.Attr.Data "role" "fieldcontain"] -< [
                    Label [Attr.For "locText"] -< [Text "Location"]
                    Input [Id "locText"; Attr.Type "text"; Attr.ReadOnly "true"; Attr.Style "background:lightgray"]

                    ]

                //claim description
                Div [HTML5.Attr.Data "role" "fieldcontain"] -< [
                    Label [Attr.For "descText"] -< [Text "Description"]
                    TextArea [Id "descText"]
                    |>! OnChange (fun x -> CurrentClaimForm.Description <- x.Value |> Some )

                    ]
            ]

    let page = 
        Div [Id "newClaim"; HTML5.Attr.Data "role" "page"] -< [
            header
            content
            ]
        |>! OnBeforeShow (fun p -> 
            match Nav.direction() with
            | Nav.Forward ->
                CurrentClaimForm.Description <- None
                CurrentClaimForm.PolicyId <- None
                CurrentClaimForm.Location <- {Lon=0.; Lat=0.}
                JQuery.Of("#descText").Val("") |> ignore
                JQuery.Of("#locText").Val("getting location...") |> ignore
                JQuery.Of("#policychoice") |> ShowBusy
            | _ -> ()
            )
        |>! OnShow (fun p ->
            match Nav.direction() with
            | Nav.Forward -> 
                LoadPolicies() |> Async.Start
                async{
                    let! pos = GetPosition()
                    let coords = pos.Coords
                    CurrentClaimForm.Location <- {Lon=coords.Longitude; Lat=coords.Latitude}
                    let posTxt = string (coords.Latitude, coords.Longitude)
                    JQuery.Of("#locText").Val(posTxt) |> ignore
                    } |> Async.Start
            | _ -> ()
            )
    page
